package com.gopay.bis.service.bank.impl.bcdl;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.SendSmsUtils;
import com.gopay.bis.manager.bank.bcdl.CmbBcdlManager;
import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacket;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.domain.bank.cmb.bcdl.CmbBcdlMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BcdlInteractRemoteService;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CmbBcdlServiceImpl extends BaseBcdlServiceImpl implements BcdlInteractRemoteService {
    @Resource(name = "cmbBcdlManager")
    CmbBcdlManager cmbBcdlManager;
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;

    private final static Logger logger = LoggerFactory.getLogger("CmbBcdlServiceImpl");

	@Override
	public BcdlResult eft(BcdlMessage base) {
		BcdlResult res = new BcdlResult();
		res.setSuccess(false);
		try {
			CmbBcdlMessage data = (CmbBcdlMessage) cmbBcdlManager.eft(base);
            logger.error("CmbBcdlServiceImpl-eft:" + LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(data)));
			res.setDefaultModel(data);
			if (data.isFlag()) {
				res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
			} else {
				//modified by zyt,2018-1-25，“是否做拒绝账务”为唯一判断失败的标准，其余非成功的均置进行中
				if(data.getDcblType() == BankConstants.DCBL_CORP){
					data.setFunName("DCPAYMNT");
				} else if(data.getDcblType() == BankConstants.DCBL_PERSON){
					data.setFunName("AgentRequest");
				}
				boolean isNeedRefund = isNeedRefund(data, base.getOrderId());
				res.setNeedRefund(isNeedRefund);
				if(isNeedRefund){
					res.setErrorMsg(data.getRetMsg());
					res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
				} else {
					res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
				}
			}

			return res;
		} catch (GopayException e) {
            if (ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())
                    || ProcCodeConstants.PROC_CODE_100E6082.equals(e.getErrCode())) {
            	String custName = getCustName(base.getOrderId());
				SendSmsUtils.sendSms(base.getGopayBankOrderNo(), smsConfig.get("switch"), "CMB",smsConfig.get("mobile"),e.getErrMsg()
						+"，商户名称："+custName+",金额："+base.getAmount().toString());
			}
			BankLogUtils.printServiceImplExceptionLog(e);
			res.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			res.setResultCode(ProcCodeConstants.PROC_CODE_100E6045);
		}
		return res;
	}


	@Override
	public BcdlResult query(BcdlMessage base) {
		BcdlResult res = new BcdlResult();
		res.setSuccess(false);
		try {
			CmbBcdlMessage data = (CmbBcdlMessage) cmbBcdlManager.query(base);
			if(CmbConstants.FIN.equals(data.getReqStatus())&&CmbConstants.CMB_BCDL_SUC.equals(data.getRetCode())){
				res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
				res.setSuccess(true);
			} else{
				//modified by zyt,2018-1-25，“是否做拒绝账务”为唯一判断失败的标准，其余非成功的均置进行中
				if(data.getDcblType() == BankConstants.DCBL_CORP){
					data.setFunName("GetPaymentInfo");
				} else if(data.getDcblType() == BankConstants.DCBL_PERSON){
					data.setFunName("GetAgentInfo");
				}
				boolean isNeedRefund = isNeedRefund(data, base.getOrderId());
				res.setNeedRefund(isNeedRefund);
				if(isNeedRefund){
					res.setErrorMsg(data.getRetMsg());
					res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
				} else {
					res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
					res.setErrorMsg(data.getRetMsg());
				}
			}
			res.setDefaultModel(data);
			return res;
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			res.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			res.setResultCode(ProcCodeConstants.PROC_CODE_100E6051);
		}
		return res;
	}

 

    @Override
    public BcdlResult queryBalance(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        res.setSuccess(false);
        try {
            CmbBcdlMessage data = (CmbBcdlMessage) cmbBcdlManager.queryBalance(base);
            if (CmbConstants.RET_SUC.equals(data.getRetCode())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
                res.setModel("payAccNum", data.getPayAccNum());
                res.setModel("balance", data.getBalance());
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setErrorMsg(data.getRetMsg());
            }

            res.setDefaultModel(data);
            return res;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6051);
        }
        return res;
    }

    @Override
    public BcdlResult sendBank(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult sendBankQueryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult eftRetry(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public BcdlResult sendBankPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public BcdlPacketRes queryPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public BcdlResult queryAcctDetail(BcdlMessage base) {
		BcdlResult result = new BcdlResult();
		result.setSuccess(false);
		try {
			CmbBcdlMessage data = (CmbBcdlMessage) cmbBcdlManager.queryAcctDetail(base);
			if (CmbConstants.RET_SUC.equals(data.getRetCode())) {
				result.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
				result.setErrorMsg("交易查询成功");
				result.setSuccess(true);
			}else {
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E5062);
				result.setErrorMsg(result.getErrorMsg());
				result.setSuccess(false);
			}
			result.setDefaultModel(data);
			return result;
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6051);
		}
		return result;
    }

}